//
//  MarketViewController.m
//  ios_news
//
//  Created by Han on 14-6-9.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "MarketViewController.h"
#import "Market.h"

@implementation MarketViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.navigationController.navigationBar.barTintColor = MAIN_COLOR;
    self.navigationController.navigationBar.translucent = NO;  // 半透明
    self.navigationController.navigationBar.hidden = NO;
    
    self.navigationItem.title = @"行情";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    
    
    // 初始数据
    mData = [NSMutableArray arrayWithCapacity:[MARKET_EN_ARRAY count]];
    for (int i = 0; i < [MARKET_EN_ARRAY count]; i++) {
        [mData addObject: [NSMutableArray arrayWithCapacity:5]];
    }
    
    
    
    mNavScrollView = [[MarketNavScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    mNavScrollView.delegateNews = self;
    [self.view addSubview:mNavScrollView];
    
    mTableViewHeight = GLOBLE_HEIGHT - 64 - 49 - 30; // nav 64 tabar 49
    
    
    mMarketScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, mTableViewHeight)];
    mMarketScrollView.delegate = self;
    mMarketScrollView.pagingEnabled = YES;  //控制控件是否整页翻动
    
    for (int i = 0; i < [MARKET_CN_ARRAY count]; i++) {
        MarketTableView *tableView = [[MarketTableView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, mTableViewHeight)];
        
        if (i == 0) {
            mCurrentTableView = tableView;
        }
        
        
        tableView.backgroundColor = ColorWithRGB(236, 236, 236);
        [tableView setTag:i + [MarketTableView getViewTag]];
        
        tableView.delegate = self;
        tableView.dataSource = self;
        //tableView.hidden = YES;
        
        
        
    
        
        [mMarketScrollView addSubview:tableView];
    }
    
    mMarketScrollView.contentSize = CGSizeMake(320 * [MARKET_CN_ARRAY count], mTableViewHeight);
    
    [self.view addSubview:mMarketScrollView];
    
    [self fristLoadData];
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //NSMutableArray *currentData = [mData objectAtIndex:mCurrentPage];
    
    //NSLog(@"currentData count = %i", [currentData count]);
    
    static NSString *CellIdentifier = @"MarketListCell";
    MarketCell *cell = (MarketCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MarketCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:CellIdentifier];
    }
    
    cell.market = [mCurrentData objectAtIndex:(NSUInteger)indexPath.row];
    
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //return [mCurrentData count];
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
    headerView.contentView.backgroundColor = UIColorFromRGB(0xe3ecf4);
    
    UILabel *leftItem = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 20)];
    leftItem.font = [UIFont systemFontOfSize:13.0f];
    leftItem.textColor = UIColorFromRGB(0x747474);
    leftItem.backgroundColor = [UIColor clearColor];
    leftItem.textAlignment = NSTextAlignmentCenter;
    leftItem.text = @"名称";
    [headerView.contentView addSubview:leftItem];
    
    UILabel *middleItem = [[UILabel alloc]initWithFrame:CGRectMake(110, 0, 100, 20)];
    middleItem.font = [UIFont systemFontOfSize:13.0f];
    middleItem.textColor = UIColorFromRGB(0x747474);
    middleItem.backgroundColor = [UIColor clearColor];
    middleItem.textAlignment = NSTextAlignmentCenter;
    middleItem.text = @"今开";
    [headerView.contentView addSubview:middleItem];
    
    UILabel *rightItem = [[UILabel alloc]initWithFrame:CGRectMake(210, 0, 100, 20)];
    rightItem.font = [UIFont systemFontOfSize:13.0f];
    rightItem.textColor = UIColorFromRGB(0x747474);
    rightItem.backgroundColor = [UIColor clearColor];
    rightItem.textAlignment = NSTextAlignmentCenter;
    rightItem.text = @"昨收";
    [headerView.contentView addSubview:rightItem];
    
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20.f;
}


- (void) loadData
{
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[MARKET_EN_ARRAY objectAtIndex:mCurrentPage], @"channel", nil];
    
    [Market getDataWithBlock:^(NSArray *data, NSError *error) {
        if (!error) {
            
            [[mData objectAtIndex:mCurrentPage] addObjectsFromArray: data];
            mCurrentData = [mData objectAtIndex:mCurrentPage];
            
            [mCurrentTableView reloadData];
            mCurrentTableView.hidden = NO;
        } else {
            NSLog(@"%@", error);
        }
    } :API_MARKET_CHANNEL :params];
    
    
    
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    
    if ([scrollView isKindOfClass:[MarketTableView class]]) {
        
        return;
    }
    
    
    [mNavScrollView changeIndicatorViewContentOffset: scrollView.contentOffset.x / 320];
}


// 减速停止了时执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    //NSLog(@"scrollViewDidEndDecelerating");
    
    if ([scrollView isKindOfClass:[MarketTableView class]]) {
        
        return;
    }
    
    
    mCurrentPage = (NSInteger) (scrollView.contentOffset.x/320);
    [self setCurrentElement];
    
    
    [self fristLoadData];
    
    [mNavScrollView adjustNavScrollView: mCurrentPage + [MarketNavScrollView getViewTag]];
    
    //NSLog(@"mCurrentPage=%li", (long)mCurrentPage);
    
}

- (void) newsScrollViewSetContentOffset:(int)currentPage
{
    mCurrentPage = (NSInteger) currentPage;
    
    [self setCurrentElement];
    
    [mMarketScrollView setContentOffset:CGPointMake(320 * currentPage, 0) animated:NO];
}


- (void) setCurrentElement {
    mCurrentData = [mData objectAtIndex:mCurrentPage];
    mCurrentTableView = (MarketTableView *) [self.view viewWithTag:mCurrentPage + [MarketTableView getViewTag]];
}


- (void) fristLoadData
{
    
    if (mCurrentTableView.mFristLoad == NO) {
        return;
    }
    
    [self loadData];
    mCurrentTableView.mFristLoad = NO;
}

@end
